<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-06-19
 * Time: 17:04
 */

namespace app\api\controller\v1;
use app\api\model\Member as MemberModel;
use app\api\model\MemberOrder;
use app\api\model\User;
use app\api\model\UserInvitationMoney;
use LinCmsTp5\exception\ParameterException;
use think\facade\Request;

class Member
{
    /**
     * 获取列表
     * @validate('PagingParameter')
     * @auth('会员列表','会员管理')
     */
    public function getDataList($page,$limit,$name,$phone,$province,$city,$county,$start,$end)
    {
        $where = [];
        if($name) {
            $where[] = [ 'name', 'like', '%'.$name.'%' ];
        }
        if($phone) {
            $where[] = [ 'phone', 'like', '%'.$phone.'%' ];
        }
        if($province) {
            $where[] = [ 'member.province', 'like', '%'.$province.'%' ];
        }
        if($city) {
            $where[] = [ 'member.city', 'like', '%'.$city.'%' ];
        }
        if($county) {
            $where[] = [ 'member.county', 'like', '%'.$county.'%' ];
        }
        if($start) {

        }
        $query = MemberModel::hasWhere('user',function ($query1) use($start,$end){
            if($start && !$end) {
                $start = strtotime($start);
                $query1->where('member_end_time','gt',$start);
            } elseif(!$start && $end){
                $end = strtotime($end);
                $query1->where('member_end_time','lt',$end);
            } elseif($start && $end){
                $start = strtotime($start);
                $end = strtotime($end);
                $query1->where('member_end_time','between',[$start,$end]);
            }
        })->where($where)
             ->with('just,back,user')
             ->order('member.id desc');
        $count = $query->count();
        $result = $query->page($page, $limit)->select()->each(function ($item){
            if($item['status'] == 1) {
                $item['status_name'] = '审核中';
            } elseif($item['status'] == 2) {
                $item['status_name'] = '审核未通过';
            } elseif($item['status'] == 3) {
                $item['status_name'] = '黑名单';
            } elseif($item['status'] == 4) {
                $item['status_name'] = '审核已通过';
            }
        })->toArray();
        $data['count'] = $count;
        $data['data'] = $result;
        return $data;
    }

    /**
     * 获取用户详细信息
     */
    public function getDataInfo($id)
    {
       $info = MemberModel::with('user')->where('id',$id)->find();
        $info['user']['end_time'] = $info['user']['member_end_time'];
       if($info['user']['end_time'] == '未支付会员') {
           $info['user']['end_time'] = '';
       }
       if($info['user']['end_time'] == '会员已过期') {
           $info['user']['end_time'] = '';
       }
       return $info;
    }

    /**
     * 修改会员日期
     */
    public function updateDate()
    {
        $data = Request::post();
        $start_time = strtotime($data['start_time'].' 00:00:00');
        $end_time = strtotime($data['end_time'].' 23:59:59');
        $member = MemberModel::get($data['id']);
        User::where('id',$member['user_id'])->update([
            'member_start_time' => $start_time,
            'member_end_time' => $end_time,
        ]);
        MemberOrder::create([
            'start_time' => $start_time,
            'end_time' => $end_time,
            'member_cost_id' => 0,
            'status' => 2,
            'member_id' => $member['id'],
            'user_id' => $member['user_id']
        ]);
        return writeJson(201, [], '操作成功！');
    }

    /**
     * 修改信息
     * @validate('checkMember')
     */
    public function updateData()
    {
        $data = Request::put();
        $info = MemberModel::with('user')->where('id',$data['id'])->find();
        if($info) {
            $mode = new MemberModel();
            $mode->allowField(true)->save($data,['id',$data['id']]);
            if($data['status'] == 4) {
                User::where('id',$info['user']['id'])->update([
                    'status' => 2
                ]);
            } else {
                User::where('id',$info['user']['id'])->update([
                    'status' => 1
                ]);
            }
            return writeJson(202, [], '操作成功！');
        } else {
            throw new ParameterException([
                'msg' => [
                    'member' => '请重新选择信息'
                ]
            ]);
        }
    }

    // 获取会员
    public function getDateList($page,$limit,$member_id)
    {
        $query = MemberOrder::where('member_id',$member_id)
            ->order('id desc');
        $count = $query->count();
        $result = $query->page($page, $limit)->select()->each(function ($item){
            if($item['member_cost_id'] == 0) {
                $item['type_name'] = '后台修改';
            } else {
                $item['type_name'] = '用户自己支付';
            }
            $item['start_time'] = date('Y-m-d',$item['start_time']);
            $item['end_time'] = date('Y-m-d',$item['end_time']);
        })->toArray();
        $data['count'] = $count;
        $data['data'] = $result;
        return $data;
    }

    // 获取用户余额列表
    public function getMoneyList($page,$limit,$member_id)
    {
        $member = MemberModel::get($member_id);
        $query = UserInvitationMoney::where('user_id',$member['user_id'])
            ->order('id desc');
        $count = $query->count();
        $result = $query->page($page, $limit)->select()->each(function ($item){
            if($item['type'] == 1) {
                $item['type_name'] = '购买会员';
            } elseif($item['type'] == 2){
                $item['type_name'] = '入住酒店';
            } elseif($item['type'] == 3){
                $item['type_name'] = '会员升级省级';
            } elseif($item['type'] == 4){
                $item['type_name'] = '会员升级市级';
            } elseif($item['type'] == 5){
                $item['type_name'] = '会员升级区/县';
            }
            if($item['status'] == 1) {
                $item['status_name'] = '得到';
            } elseif($item['status'] == 2) {
                $item['status_name'] = '提现';
            }
        })->toArray();
        $data['count'] = $count;
        $data['data'] = $result;
        return $data;
    }
}